<template>
  <div>
    <SearchContainer @search="getData" @reset="() => formRef.resetFields()">
      <a-form ref="formRef" class="form-style-flex" :model="formState" :label-col="{ style: { width: '80px' } }">
        <a-form-item name="searchWord" label="" class="w-72">
          <a-input v-model:value="formState.searchWord" placeholder="名称" />
        </a-form-item>
      </a-form>
    </SearchContainer>
    <div class="mt-2">
      <a-button type="primary" @click="onEdit()"> 新增 </a-button>
    </div>
    <a-card class="mt-2 flex-1">
      <BaseTable :loading="loading" column-code="001100" :scroll="{ x: 2400 }" fill :data-source="tableData" bordered :pagination="pagination">
        <template #bodyCell="{ column, index, record }">
          <template v-if="column.dataIndex === 'xh'">
            {{ index + 1 }}
          </template>
          <template v-if="column.dataIndex === 'status'">
            <a-switch v-model:checked="record.status" :un-checked-value="0" :checked-value="1" @change="onChangeSwitch(record)" />
          </template>
          <template v-if="column.dataIndex === 'action'">
            <a-button type="link" size="small" @click="onEdit(record)"> 修改 </a-button>
            <BaseButton confirm-text="确认删除？" danger size="small" type="link" @click="onDelete(record)">删除</BaseButton>
            <a-button type="link" size="small" @click="onControl(record)"> 对照 </a-button>
            <a-button type="link" size="small" disabled @click="onImportTemplate(record)"> 导入模板 </a-button>
          </template>
        </template>
      </BaseTable>
    </a-card>
    <Edit v-model:open="open" :row-data="rowData" @ok="getData" />
    <TemplateSelection v-model:open="templateOpen" :row-data="rowData" @ok="getData" />
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'

import { message } from 'ant-design-vue'
import SearchContainer from '@/components/SearchContainer'
import Edit from './Edit'
import TemplateSelection from './TemplateSelection.vue'

const formRef = ref(null)
const router = useRouter()

const open = ref(false)
const templateOpen = ref(false)
const rowData = ref({})

const formState = ref({
  searchWord: ''
})
const onControl = (item) => {
  router.push({
    path: '/workstation-technology/setting/classify-control',
    query: {
      categoryCode: item.categoryCode
    }
  })
}
const onImportTemplate = (item) => {
  templateOpen.value = true
  rowData.value = item
}
const onEdit = (item = {}) => {
  open.value = true
  rowData.value = { ...item }
  rowData.value.deptCode = rowData.value.deptCode?.split(',')
}
const onChangeSwitch = async (item) => {
  const { code, msg } = await post('/ris/dictCategory/doChangeRisCategoryStatus', { data: item.categoryCode })
  if (code !== 0) return message.error(msg)
  message.success('操作成功')
}
const onDelete = async (item) => {
  const { code, msg } = await post('/ris/dictCategory/doDeleteRisCategory', { data: item.categoryCode })
  if (code !== 0) return message.error(msg)
  message.success('删除成功')
  getData()
}
const { loading, tableData, pagination, getData } = useTable({
  url: '/ris/dictCategory/getDictCategoryList',
  params: formState.value,
  formatParams: (params) => {
    return {
      stdate: params['dateRange']?.[0],
      eddate: params['dateRange']?.[1],
      ...params
    }
  }
})

onMounted(() => {
  getData()
})
</script>
